'use strict';
import {
    StyleSheet,
    View,
    TouchableOpacity,
    Navigator,
    AlertIOS,
    ScrollView,
    ActionSheetIOS,
    NavigatorIOS,
    ListView,
    Text,
    UIManager,
    TextInput
} from 'react-native';
import React from 'react';
import Icon from 'react-native-vector-icons/Ionicons';
import URL from '../config/server';
import {Utils} from '../config/utils';
import {Key} from '../config/key';
// import PasteBoard from 'react-native-pasteboard';
const shareMessage = '分享内容：我在用神秘涨停网炒股，天天涨停。大家赶紧填写我的邀请码注册，一起做涨停的股票。软件下载(点击):安卓版:360手机助手(官方指定)应用宝  百度手机助手 苹果版: 苹果商城(app store )';

const NavigationBarRouteMapper = {
    LeftButton(route, navigator, index, navState) {
        return (
            <TouchableOpacity style={{flex: 1, justifyContent: 'center'}}
                              onPress={() => navigator.parentNavigator.pop()}>
                <View style={{flexDirection:'row',padding:5}}>
                    <Icon name="ios-arrow-back" size={18} color="#ffffff" style={{alignSelf:'center'}}/>
                    <Text style={{color: 'white', margin: 3,fontSize:16}}>
                        返回
                    </Text>
                </View>
            </TouchableOpacity>
        );
    },
    RightButton(route, navigator, index, navState) {
        return ( 
            <TouchableOpacity 
                style={{flex: 1, justifyContent: 'center'}}
                onPress={() => {
                    // Take the snapshot (returns a temp file uri)
                    // UIManager.takeSnapshot('window').then((uri) => {
                    //   // Share image data
                      
                    // }).catch((error) => alert(error));
                    ActionSheetIOS.showShareActionSheetWithOptions({
                        url: 'https://itunes.apple.com/us/app/shen-mi-zhang-ting-wang/id1073503577?ls=1&mt=8',
                        excludedActivityTypes: [
                          'com.apple.UIKit.activity.PostToTwitter'
                        ],
                        message:shareMessage
                      },
                      (error) => alert(error),
                      (success, method) => {
                            var text;
                            if (success) {
                              text = `Shared via ${method}`;
                            } else {
                              text = 'You didn\'t share';
                            }
                            // this.setState({text});
                    });
                }}>
                <View style={{flexDirection:'row',padding:5}}>
                    <Text style={{color: 'white', margin: 3,fontSize:16}}>
                        分享
                    </Text>
                </View>
            </TouchableOpacity>
        );
    },
    Title(route, navigator, index, navState) {
        return (
            <Text style={{color: 'white', margin: 10, fontSize: 20}}>
                推广中心
            </Text>
        );
    }
};

export default class MySpread extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            inviteCode: '',
            dataSource: new ListView.DataSource({
                rowHasChanged: (row1, row2) => row1 !== row2
            }),
        };
    }

    componentWillMount() {
        let that = this;
        const INVITE = URL.local + 'invited';
        Utils.transport(INVITE, '', function (data) {
            console.log(data);

            that.setState({
                inviteCode: data.invitecode,
                dataSource: that.state.dataSource.cloneWithRows(data.invitelist)
            })
        })
    }


    _renderRow(rowData, rowID, highlightRow) {
        return (
            <View style={styles.item}>
                <Text style={styles.itemText}>{parseInt(highlightRow) + 1}.{'\t'}{rowData}</Text>
            </View>
        )
    }

    _pasteText() {
        PasteBoard.copyText(this.state.inviteCode, (callback) => {
            AlertIOS.alert('温馨提示', '您的邀请码已复制到系统剪贴板.');
        });
    }

    renderScene() {
        return (
            <View style={styles.container}>
                <Text style={styles.h2}>
                    您的推广邀请码:
                </Text>
                <Text style={styles.h1}>
                    {this.state.inviteCode}

                </Text>
                <Text style={styles.p}>
                    请您将带有推广码的页面截图，然后把此图片发到朋友圈.您的朋友注册此软件就能帮助你完成升级.
                </Text>
                <Text style={styles.h3}>已邀请好友列表</Text>
                <ListView
                    style={styles.listStyle}
                    dataSource={this.state.dataSource}
                    renderRow={this._renderRow}
                />
            </View>
        );
    }

    render() {
        return (
            <Navigator
                renderScene={this.renderScene.bind(this)}
                navigator={this.props.navigator}
                navigationBar={
            <Navigator.NavigationBar 
                style={{backgroundColor: '#e02820', alignItems: 'center'}}
                routeMapper={NavigationBarRouteMapper} />
          }/>
        );
    }
}


const styles = StyleSheet.create({
    nav: {
        flex: 1
    },
    container: {
        flex: 1,
        backgroundColor: '#f5f5f9',
        paddingTop: 100
    },
    item: {
        padding: 10,
        borderBottomWidth: 0.5,
        borderColor: '#a9a9a9'
    },
    itemText: {
        fontSize: 17,
        textAlign: 'left',
        paddingLeft: 30
    },
    listStyle: {
        backgroundColor: '#ffffff',
        borderTopWidth: 1,
        borderStyle: 'dotted',
        borderColor: '#a9a9a9'
    },
    h2: {
        color: '#e02820',
        textAlign: 'center',
        fontSize: 18,
        padding: 10
    },
    h1: {
        textAlign: 'center',
        fontSize: 30,
        paddingBottom: 20
    },
    p: {
        color:'#ff0000',
        textAlign: 'justify',
        fontSize: 13,
        padding: 20,
        paddingTop:0
    },
    h3: {
        backgroundColor: '#ffffff',
        color: '#000000',
        textAlign: 'center',
        fontSize: 16,
        padding: 10
    }
});

